Page({
  data: {
    mqtt: {
      uid: 'c7e9d9637de217b40bae8d1afd279e31',
      url_push: 'https://api.bemfa.com/api/device/v1/data/3/push/get/',
      topic_cmd: 'WCtoESP',
      topic_log: 'LOG'
    },
    input: {
      id: undefined,
      password: undefined,
      time_start: undefined,
      time_end: undefined
    },
    userInfo: [],
    userRequest: [],
    userRequestReply: [],
    user: undefined,
    isLogin: false,
    espStatus: false
  },
  mqttSend: function(topic, msg) {
    let that = this
    wx.request({
      url: that.data.mqtt.url_push,
      data: {
        uid: that.data.mqtt.uid,
        topic: topic,
        msg: msg
      },
      success(res) {
        console.log('[user] 向Topic[' + topic + ']推送消息:')
        console.log(res)
      },
      fail() {
        console.log('[user] HTTPS请求失败！')
      }
    })
  },
  onShow: function() {
    let that = this
    wx.getStorage({
      key: 'userInfo',
      success(res) {
        if(res.data != undefined) {
          console.log('[user] 获得用户数据:')
          console.log(res.data)
          that.setData({
            userInfo: wx.getStorageSync('userInfo')
          })
        }
      },
      fail() {
        console.log('[user] 获得用户数据失败!')
      }
    })
    wx.getStorage({
      key: 'userRequest',
      success(res) {
        if(res.data != undefined) {
          console.log('[user] 获得用户申请:')
          console.log(res.data)
          that.setData({
            userRequest: wx.getStorageSync('userRequest')
          })
        }
      },
      fail() {
        console.log('[user] 获得用户申请失败!')
      }
    })
    wx.getStorage({
      key: 'userRequestReply',
      success(res) {
        if(res.data != undefined) {
          console.log('[user] 获得用户申请回复:')
          console.log(res.data)
          that.setData({
            userRequestReply: wx.getStorageSync('userRequestReply')
          })
        }
      },
      fail() {
        console.log('[user] 获得用户申请回复失败!')
      }
    })
  },
  onHide: function() {
    if(this.data.isLogin) {
      this.setData({isLogin: false})
      this.mqttSend(this.data.mqtt.topic_log, '用户[' + this.data.user.name + '(id:' + this.data.user.id + ')' + ']已下线')
    }
  },
  bindInputID: function(e) {
    this.data.input.id = e.detail.value
    this.setData({input: this.data.input})
  },
  bindInputPassword: function(e) {
    this.data.input.password = e.detail.value
    this.setData({input: this.data.input})
  },
  bindChangeTimeStart: function(e) {
    this.data.input.time_start = e.detail.value
    this.setData({input: this.data.input})
  },
  bindChangeTimeEnd: function(e) {
    this.data.input.time_end = e.detail.value
    this.setData({input: this.data.input})
  },
  bindButtonLoginTap: function() {
    if(this.data.input.id == undefined) {
      wx.showToast({
        title: '请输入用户ID',
        icon: 'error',
        duration: 2000
      })
    } else if(this.data.input.password == undefined) {
      wx.showToast({
        title: '请输入用户密码',
        icon: 'error',
        duration: 2000
      })
    } else {
      this.setData({user: this.data.userInfo.find(item => item.id == this.data.input.id)})
      if(this.data.user) {
        this.data.input.time_start = this.data.user.time_start
        this.data.input.time_end = this.data.user.time_end
        this.setData({input: this.data.input})
        if(this.data.input.password == this.data.user.password) {
          this.setData({isLogin: true})
          this.mqttSend(this.data.mqtt.topic_log, '用户[' + this.data.user.name + '(id:' + this.data.user.id + ')' + ']已上线')
          let user = this.data.userRequestReply.find(item => item.id == this.data.user.id)
          if(user) {
            this.data.userRequestReply.splice(this.data.userRequestReply.findIndex(item => item.id === this.data.user.id), 1)
            wx.setStorage({
              key: 'userRequestReply',
              data: this.data.userRequestReply,
              success () {
                wx.getStorage({
                  key: 'userRequestReply',
                  success(res){
                    console.log('[user] 存储用户申请回复:')
                    console.log(res.data)
                  }
                })
              },
              fail() {
                console.log('[user] 存储用户申请回复失败！')
              }
            })
            if(user.reply) {
              wx.showModal({
                title: '通知',
                content: '您于时间段 [' + user.time_start + '-' + user.time_end + ']  内的访问申请已批准',
                showCancel: false
              })
            } else {
              wx.showModal({
                title: '通知',
                content: '您于时间段 [' + user.time_start + '-' + user.time_end + ']  内的访问申请已被拒绝',
                showCancel: false
              })
            }
          }
        } else {
          wx.showToast({
            title: '密码错误',
            icon: 'error',
            duration: 2000
          })
        }
      } else {
        wx.showToast({
          title: 'ID未注册',
          icon: 'error',
          duration: 2000
        })
      }
    }
  },
  bindButtonChangeTimeTap: function() {
    if(this.data.user.time_start == this.data.input.time_start && this.data.user.time_end == this.data.input.time_end) {
      wx.showToast({
        title: '该时间段已授权',
        icon: 'error',
        duration: 2000
      })
    } else {
      this.data.userRequest.splice(this.data.userInfo.findIndex(item => item.id === this.data.user.id), 1)
      this.data.userRequest.push({
        id: this.data.user.id,
        name: this.data.user.name,
        time_start: this.data.input.time_start,
        time_end: this.data.input.time_end
      })
      this.setData({
        userRequest: this.data.userRequest
      })
      wx.setStorage({
        key: 'userRequest',
        data: this.data.userRequest,
        success () {
          wx.getStorage({
            key: 'userRequest',
            success(res){
              console.log('[user] 存储用户申请:')
              console.log(res.data)
            }
          })
        },
        fail() {
          console.log('[user] 存储用户申请失败！')
        }
      })
      console.log('[user] 提交用户申请:')
      console.log(this.data.userRequest.find(item => item.id == this.data.user.id))
      wx.showToast({
        title: '申请已发送',
        icon: 'success',
        duration: 2000
      })
    }
  },
  bindButtonEnrollTap: function() {
    this.mqttSend(this.data.mqtt.topic_cmd, {
      cmd: "enroll",
      id: this.data.user.id,
      name: this.data.user.name,
      sh: parseInt(this.data.user.time_start.slice(0, 2)),
      sm: parseInt(this.data.user.time_start.slice(3, 5)),
      eh: parseInt(this.data.user.time_end.slice(0, 2)),
      em: parseInt(this.data.user.time_end.slice(3, 5))
    })
    wx.showToast({
      title: '指令已发送',
      icon: 'success',
      duration: 2000
    })
  },
  bindButtonQuitTap: function() {
    this.setData({
      input: {
        id: undefined,
        password: undefined,
        time_start: undefined,
        time_end: undefined
      },
      isLogin: false
    })
  }
})
